package org.jackysoft.ireporter;

import java.io.File;
import java.io.FileFilter;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class IReporterTool {
	private static final Log log = LogFactory.getLog(IReporterTool.class);

	public static void compileReport(File jrbase) {

		if (!jrbase.isDirectory())
			throw new IllegalStateException("路径" + jrbase.getAbsolutePath()
					+ "不是一个目录");
		File[] jrxmls = jrbase.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(".jrxml");
			}
		});
		log.info("开始编译jasper报表.......");
		for (File jr : jrxmls)
			compileFile(jr);
		log.info("编译jasper报表完成");
	}

	static void compileFile(File srcPath) {
		try {

			log.info(srcPath.getAbsolutePath());
			String jap = JasperCompileManager.compileReportToFile(srcPath
					.getAbsolutePath());
			log.info(jap + "  完成!");

		} catch (JRException e) {
			log.info("编译jasper报表出错 : \n\r" + e);

			e.printStackTrace();
		}
	}
}
